package edu.hrbeu.controller;

import edu.hrbeu.entity.Discount;
import edu.hrbeu.mapper.CategoryMapper;
import edu.hrbeu.service.CategoryService;
import edu.hrbeu.service.DiscountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author wzq
 * @create 2022-01-06-12:09
 */
@Controller
@RequestMapping(value = "/discount")
public class DiscountController {

    @Autowired
    CategoryService categoryService;

    @Autowired
    DiscountService discountService;

    @GetMapping("/list")
    public ModelAndView toList(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("discount/list");
        mv.addObject("discountList",discountService.list());
        return mv;
    }
    @GetMapping("/add")
    public ModelAndView toAdd(){
        ModelAndView mv = new ModelAndView("discount/add");
        mv.addObject("categoryList",categoryService.list());
        return mv;
    }

    @PostMapping("/add")
    public String Add(Discount discount) {
      discountService.save(discount);
      return "redirect:/discount/list";
    }

    @GetMapping("/delete")
    public String delete(Integer id){
        discountService.removeById(id);
        return "redirect:/discount/list";
    }

    @RequestMapping("/editState")
    public String editState(Discount discount){
        if(discount.getState()){
            discountService.enable(discount);
        }else{
            discountService.disenable(discount);
        }
        return "redirect:/discount/list";
    }
}
